package 异常机制详解.异常基础;

/**
 * @program: algorithm
 * @description: 测试异常抛出
 * @author: 陈恩涛
 * @create: 2021-12-22 22:04
 **/
public class TestExceptionThrow {
    public static void main(String[] args) {
        System.out.println(test2());
        System.out.println(foo());
        test1();
    }

    public static void test1() {
        int i = 0;
        try {
            if (i == 0) {
                throw new RuntimeException("参数不能为0");
            }
        } catch (RuntimeException e) {
            System.out.println("进入catch代码块");
            throw e;
        } finally {
            System.out.println("进入finally代码块");
        }
    }

    public static int test2() {
        int i = 0;
        try {
            return ++i;
        } finally {
            return ++i;
        }
    }

    public static int foo() {
        int x = 0;
        try {
            x = 1;
            return x;
        } catch (Exception e) {
            x = 2;
            return x;
        } finally {
            x = 3;
        }
    }


}
